var moldTypeId = getQueryString("moldTypeId")
$(function () {
    vm.moldTypeId = moldTypeId;
    $("#jqGrid").Grid({
        url: '../moldTypePosition/list?moldTypeId=' + moldTypeId,
        rownumWidth: 35,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '备件名称', name: 'positionName', index: 'POSITION_NAME', width: 90},
            {label: '位置号', name: 'positionNo', index: 'POSITION_NO', width: 60},
            {label: '规格', name: 'spec', index: 'SPEC', width: 60},
            {label: '类型', name: 'modType', index: 'MOD_TYPE', width: 60},
            {label: '适用型芯规则', name: 'barCodeRange', index: 'BAR_CODE_RANGE', width: 60},
            {label: '寿命标准', name: 'lifeStandard', index: 'LIFE_STANDARD', width: 60},
            {label: '特殊处理', name: 'positionRemark', index: 'POSITION_REMARK'},
            {name: 'currentTime', hidden: true}
        ]
    });
    vm.getMoldPositionTypes();
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        modulPositionCorrelation: {
            modType: '',
            lifeStandard: '',
            residualLife: '',
            changeTime: '',
            positionRemark: '',
            positionName: '',
            positionNo: '', // 位置号
            spec: '', // 规格
            barCodeRange: '', // 适用型芯条码
            id: '',
        },
        ruleValidate: {
            positionName: [
                {required: true, message: '备件名称必填', trigger: 'blur'}
            ],
            positionNo: [
                {required: true, message: '位置号必填', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            positionNo: '', // 位置号
            modType: ''
        },
        moldTypeId: '', // 模具ID
        moduleTypeList: enumsToList('MODULE_SPAREPART'), //模具 备件更换 镶块、型芯、镶件
        moldPositionTypes: []
    },
    methods: {
        getMoldPositionTypes: function () {
            // 获取备件单位
            Ajax.request({
                url: '../moldTypePosition/listUnit',
                async: false,
                successCallback: function (r) {
                    vm.moldPositionTypes = r.moldPositionTypes;
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.modulPositionCorrelation = {
                modType: '',
                lifeStandard: 10000,
                positionRemark: '',
                positionName: '',
                positionNo: '', // 位置号
                spec: '', // 规格
                barCodeRange: '',
                moldTypeId: moldTypeId,
                id: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.modulPositionCorrelation.id == (null || '') ? "../moldTypePosition/save" : "../moldTypePosition/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.modulPositionCorrelation),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../moldTypePosition/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../moldTypePosition/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.modulPositionCorrelation = r.modulPositionCorrelation;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            // 清空postData缓存数据
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            // 构建查询参数
            var param = {};
            if (vm.q.name) {
                param.name = vm.q.name;
            }
            if (vm.q.modType) {
                param.modType = vm.q.modType;
            }
            if (vm.q.positionNo) {
                param.positionNo = vm.q.positionNo;
            }
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: param,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                positionNo: '', // 位置号
                modType: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles();
            this.$Loading.start();
        },
        handleSuccess: function (res, file) {
            if (res.code == 0) {
                this.$Loading.finish();
                alert(res.msg, function (index) {
                    vm.reload();
                });
            } else {
                this.$Loading.error();
                this.$Notice.error({
                    title: '导入失败',
                    desc: res.msg
                })
            }
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls或.xlsx的文件。'
            });
        },
        handleError: function (err, file) {
            alert('导入失败');
        },
        //数据导出
        exportData: function () {
            window.open('../moldTypePosition/export?moldTypeId=' + this.moldTypeId);
        }
    }
});
